package org.douxiaoban.client.api;

import org.douxiaoban.client.activity.OneMailInfoActivity;
import org.douxiaoban.client.activity.SendMailActivity;
import org.douxiaoban.client.adapter.MailAdapter;
import org.douxiaoban.client.app.Assistant;
import org.douxiaoban.client.util.ThreadTool;
import org.douxiaoban.client.widget.PopupRecommend;
import org.douxiaoban.data.MailData;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AdapterView;
/**
 * Mail 逻辑处理类
 * @author 6a209qt
 *
 */
public class MailList extends DoubanList{
	
	private Context mContext;
	
	
	private final static int REP_MAIL = 0;
	private final static int DEL_MAIL = 1;
	
	public final static int INBOX = 0;
	public final static int OUTBOX = 1;
	
	private int mCurrentIndex;
	private ThreadTool mThread;
	private PopupRecommend mPopup;
	
	private int whichBox;
	
	public MailList(Context context, MailAdapter adapter, DoubanListType type){
		super(context, adapter, type);
		mContext = context;
		mThread = ThreadTool.instace(mContext);
		mPopup = new PopupRecommend(context);
		whichBox = DoubanListType.MAIL_INBOX == type ? INBOX : OUTBOX;
	}

	
	@Override
	public boolean longClickEvent(AdapterView<?> arg0, View arg1, int index,
			long id) {
//		if(0 == index--){
//			return false;
//		}
		final MailData mailData = (MailData) getAdapter().getItem(index);
		final int listIndex = index;
		DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface arg0, int posDlg) {
				// TODO Auto-generated method stub
				//clickItem(posList, posDlg);
				sendIntent(mailData, posDlg, listIndex);
			}
		};
		//对话框的两个选项
		String [] listStr = new String[]{"回应","删除"};
		
		new AlertDialog.Builder(mContext)
		.setTitle("操作菜单")
		.setItems(listStr, listener).show();
		return true;
	}

	@Override
	public void clickEvent(AdapterView<?> arg0, View arg1, int index, long id) {
		
//		if(0 == index--){
//			refresh();
//			return;
//		}
		MailData mailData = (MailData)this.getAdapter().getItem(index);
		toMailInfo((Activity)mContext, mailData, index);
	}
	
	private Handler mHandler = new Handler(){
		@Override
		public void handleMessage(Message msg){
			Bundle bundle = msg.getData();
			switch(msg.what){
				case Assistant.MSG_DEL_MAIL:
					if(bundle.getBoolean(Assistant.KEY_SUCCESS)){
						//提示删除成功					
						int index = bundle.getInt(Assistant.KEY_MAIL_INDEX);
						removeOneMail(index);
						mPopup.showToastWindow(Assistant.SUCCESS_POP, "成功删除豆邮");
					}else{
						//删除不成功
						mPopup.showToastWindow(Assistant.FAIL_POP, "删除豆邮失败");
					}
				default:
					break;
			}
		}
	};
		
	private void toMailInfo(Activity ac, MailData mailData, int index){
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		bundle.putString(Assistant.KEY_MAIL_ID, String.valueOf(mailData.mId));
		bundle.putBoolean(Assistant.KEY_IS_INBOX, mailData.mIsInbox);
		intent.putExtras(bundle);
		intent.setClass(mContext, OneMailInfoActivity.class);
		mCurrentIndex = index;
//		mCurrentBox = mailData.mIsInbox;
		mailData.mUnread = false;
		setToRead();
		ac.startActivityForResult(intent, whichBox);
	}
	
	public int getCurrentIndex() {
		return mCurrentIndex;
	}


//	public void setCurrentIndex(int mCurrentIndex) {
//		this.mCurrentIndex = mCurrentIndex;
//	}


	private void toRepMail(MailData mailData){
		Intent intent = new Intent();
		Bundle bundle = new Bundle();
		String userId = mailData.mIsInbox ? mailData.mSenderId : mailData.mRecName;
		bundle.putString(Assistant.KEY_MAIL_ID, userId);
		String name = mailData.mIsInbox ? mailData.mSenderName : mailData.mRecName;
		bundle.putString(Assistant.KEY_USER_NAME, name);
		intent.putExtras(bundle);
		intent.setClass(mContext, SendMailActivity.class);
		mContext.startActivity(intent);
	}
	
	
	
	
	/**
	 * 在ui上删除某封邮件
	 * @param mailIndex 要删除的邮件的位置
	 * @param isInbox 是否是收件箱
	 */
	public void removeOneMail(int index){
		if( -1 == index){
			return;
		}
		this.getAdapter().remove(index);
	}
	
	/**
	 * 刷新
	 * 在设置豆邮为以读时
	 */
	public void setToRead(){
		getAdapter().notifyDataSetChanged();
	}
	
	private void sendIntent(MailData data,  int posDlg, int listIndex){
		if(REP_MAIL == posDlg){
			//回应豆油
			toRepMail(data);
		}else if(DEL_MAIL == posDlg){
			// 删除豆油
			mThread.delMail(mHandler, String.valueOf(data.mId), listIndex); 
		}
	}
	
}